package com.controller;

import com.model.BrowRecored;
import com.model.User;
import com.model.VariesCount;
import com.service.BookService;
import com.service.UserService;
import com.service.VariesService;
import com.util.BrowRecoredVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("bc")
public class BrowController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @Autowired
    private VariesService variesService;
    //插入一条借书记录
    @RequestMapping("browbook.do")
    @ResponseBody
    public HashMap BrowBook(HttpServletRequest request, String bookId){
        HashMap map=new HashMap();
        int bId=Integer.parseInt(bookId);
        //获取当前用户id
        HttpSession session=request.getSession();
        User user_db=(User)session.getAttribute("user");
        Integer creditScore = user_db.getCreditScore();
        int userid=user_db.getUserid();
        System.err.println("userid = " + userid);
        //查询该用户的当前可借阅数
        int borrowNumber=userService.queryBorrowNumberByUserId(userid);
        System.out.println("borrowNumber = " + borrowNumber);
        //查询当前图书的库存
        int bookid=Integer.parseInt(bookId);
        int bookNumber=bookService.queryBookNumber(bookid);
        if(creditScore<50){
            map.put("info","creditScore");
        }else {
            if (bookNumber<0){
                map.put("info","kucun");
            }else {
                //判断该用户的当前可借阅数是否为0
                if (borrowNumber > 0) {
                    int i = bookService.insertSelective(userid, bId);
                    System.err.println("i = " + i);

                    if(i>0){
                        map.put("info","SUCCESS");
                    }
                }else{
                    map.put("info","num");
                }
            }
        }


        return map;
    }
    //管理员修改借书记录UI
    @RequestMapping("forbrowbookUI.do")
    public String SuccessBrowBookUI(){
        return "forbrowList";
    }
    //借阅申请
    @RequestMapping("forbrowbookList.do")
    @ResponseBody
    public HashMap<String,Object> ForBrowBookList(String page,String limit){
        HashMap<String ,Object> map = new HashMap<>();
        HashMap<String,Object> browMap = bookService.queryForBrowList(page,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",browMap.get("total"));
        List<BrowRecoredVO> l= (List<BrowRecoredVO>) browMap.get("list");
        for (BrowRecoredVO browRecoredVO : l) {
            System.err.println(browRecoredVO.getBrowDate());
        }

        map.put("data",browMap.get("list"));
        return map;
    }
    //管理员修改借书记录
    @RequestMapping("successbrowbook.do")
    @ResponseBody
    public HashMap SuccessBrowBook(String browRecoredId,String bookId,String userId) throws ParseException {
        System.out.println("正在修改");
        DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= sdf.format( new Date());
        System.out.println("time = " + time);
        int brid=Integer.parseInt(browRecoredId);
        System.out.println("brid = " + brid);
        //查询该用户的当前可借阅数
        int userid=Integer.parseInt(userId);
        System.out.println("userid = " + userid);
        int borrowNumber=userService.queryBorrowNumberByUserId(userid);
        System.out.println("borrowNumber = " + borrowNumber);
        HashMap map=new HashMap();
        if(borrowNumber>0){
            //修改图书状态为1
            int i = bookService.updateByBookState(brid, time);
            int bId=Integer.parseInt(bookId);
            System.out.println("bId = " + bId);
            //该图书库存减一
            int j=bookService.updateStockNumberByBookId(bId);
            //用户可借图书数量减一
            int z=userService.updateBorrowNumber(userid,borrowNumber);
            System.out.println("z = " + z);
            System.out.println("j = " + j);
            System.out.println("i = " + i);
            if(i>0){
                map.put("info","SUCCESS");
            }else {
                map.put("info","FAIL");
            }
        }else {
            map.put("info","FAIL");
        }
        return map;
    }

    //用户历史借阅记录UI
    @RequestMapping("beforeBrowBookUI.do")
    public String BeforeBrowBookUI(){
        return "beforeBrowList";
    }
    //用户历史借阅记录
    @RequestMapping("beforeBrowBookList.do")
    @ResponseBody
    public HashMap<String,Object> BeforeBrowBookList(HttpServletRequest request,String page,String limit){
        System.out.println("page " + page + " limit:" + limit);
        System.out.println("查询开始");
        //获取当前用户id
        HttpSession session=request.getSession();
        User user_db=(User)session.getAttribute("user");
        int userid=user_db.getUserid();
        System.out.println("userid = " + userid);
        HashMap<String,Object> browRecoredMap = bookService.queryBeforeBrowBookList(userid,page,limit);
        HashMap map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",browRecoredMap.get("total"));

        System.err.println(browRecoredMap.get("total")+"----------browRecoredMap.get(\"total\");----------");
        map.put("data",browRecoredMap.get("list"));
        return map;
    }
    //借阅偏好
    @RequestMapping("browBookLikeUI.do")
    public String BrowBookLikeUI(){
        return "browBookLike";
    }
    @RequestMapping("browBookLike.do")
    @ResponseBody
    public List<BrowRecored> BrowBookLike(){
        List<BrowRecored> browRecoreds = bookService.queryNowAndBeforeBrowList();
        System.out.println("browRecoreds = " + browRecoreds);
        return browRecoreds;
    }



    //统计当前和历史借阅记录中的每种图书的数量，用ajax请求数据 并绘制雷达图
    @RequestMapping("variesCount.do")
    @ResponseBody
    public HashMap<String,Object> VariesCount(){
        List<VariesCount> list=variesService.queryVariesCount();
        //"校园","爱情","叛逆","悬疑","惊悚","网络"," 爆笑"," 魔幻"
        String[] name={"校园","爱情","叛逆","悬疑","惊悚","网络","爆笑","魔幻"};
        int[] num={0,0,0,0,0,0,0,0};
        for (VariesCount variesCount : list) {
            if (variesCount.getVariesName().equals("校园")){
                num[0]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("爱情")){
                num[1]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("叛逆")){
                num[2]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("悬疑")){
                num[3]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("惊悚")){
                num[4]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("网络")){
                num[5]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("爆笑")){
                num[6]=variesCount.getVariesNum();
            }
            if (variesCount.getVariesName().equals("魔幻")){
                num[7]=variesCount.getVariesNum();
            }
        }
        System.out.println("name = " + Arrays.toString(name));
        System.out.println("num = " +Arrays.toString(num));
        HashMap map=new HashMap();
        map.put("name",name);
        map.put("num",num);
        return map;
    }
}
